package com.huangliusong;

import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.client.transport.TransportClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;


@SpringBootApplication
@RestController
public class BookApplication {

	@Autowired
	private TransportClient client;
	@GetMapping("/")
	public String index() {
		return "index";
	}
	
	@SuppressWarnings({ "rawtypes", "unchecked" })
	@GetMapping("/get/prople/name")
	@ResponseBody
	public ResponseEntity get(@RequestParam(name="name") String name) {
		if(name.isEmpty()) {
			return new ResponseEntity(HttpStatus.NOT_FOUND);
		}
		GetResponse response=this.client.prepareGet("people","man",name).get();
		if(!response.isExists()) {
			return new ResponseEntity(HttpStatus.NOT_FOUND);
		}
		return new ResponseEntity(response.getSource(),HttpStatus.OK);
	}
	public static void main(String[] args) {
		SpringApplication.run(BookApplication.class, args);
	}
}
